package com.yungu.swift.admin.config;

import com.yungu.swift.admin.base.CurrentUserHandlerMethodArgumentResolver;
import com.yungu.swift.admin.handler.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

/**
 * @author xiuyin.cui
 * @Description
 * @date 2019/3/21 9:49
 */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private LogHandler logHandler;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 限流方案
        registry.addInterceptor(new RateLimitHandler())
                .addPathPatterns("/admin/**");
        // accessToken 校验
        registry.addInterceptor(new TokenHandler())
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/v1/common/login")
                .excludePathPatterns("/admin/v1/common/logout")
                .excludePathPatterns("/admin/v1/common/refreshToken")
                .excludePathPatterns("/admin/v1/common/identify")
                .excludePathPatterns("/admin/v1/system/permission/user/resetPassword")
                .excludePathPatterns("/admin/v1/marketing/article/detail")
                .excludePathPatterns("/admin/v1/marketing/article/visit/count");

        // 签名校验
        registry.addInterceptor(new SignHandler())
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/v1/common/*/upload")
                .excludePathPatterns("/admin/v1/operation/*/import")
                .excludePathPatterns("/admin/v1/operation/*/uploadPictures")
                .excludePathPatterns("/admin/v1/marketing/coupon/gift/uploadCdkey")
                .excludePathPatterns("/admin/v1/marketing/article/detail")
                .excludePathPatterns("/admin/v1/marketing/article/visit/count");

        // 需要去掉图片前缀的接口
        registry.addInterceptor(new ImgPrefixHandler())
                .addPathPatterns("/admin/v1/operation/car/saveOrUpdate")
                .addPathPatterns("/admin/v1/operation/driver/saveOrUpdate")
                .addPathPatterns("/admin/v1/operation/platform/saveOrUpdate")
                .addPathPatterns("/admin/v1/operation/driver/check/pass")
                .addPathPatterns("/admin/v1/operation/passenger/message/saveOrUpdate")
                .addPathPatterns("/admin/v1/marketing/coupon/gift/saveOrUpdate")
                .addPathPatterns("/admin/v1/order/join/create");
        //日志跟踪
        registry.addInterceptor(logHandler);
        super.addInterceptors(registry);
    }

    /**
     * 解决跨域访问问题
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedHeaders("Content-Type")
                .allowCredentials(true);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new CurrentUserHandlerMethodArgumentResolver());
    }
}
